package com.sys.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sys.platform.common.web.controller.BaseController;
import com.sys.platform.common.web.domain.AjaxResult;
import com.sys.platform.common.web.page.TableDataInfo;
import com.sys.platform.entity.Camera;
import com.sys.platform.entity.DossierDevice;
import com.sys.platform.entity.request.DossierDevice.DossierDeviceDTO;
import com.sys.platform.entity.request.DossierDevice.DossierDeviceOpenOrCloseDTO;
import com.sys.platform.entity.vo.DossierDevice.DossierDeviceVO;
import com.sys.platform.service.ICameraService;
import com.sys.platform.service.IDossierDeviceService;
import com.sys.platform.service.IFavoritesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 档案馆设备Controller
 *
 * @author yuanfuqiang
 * @date 2024-01-10
 */
@Api(tags = "档案馆设备管理")
@RestController
@RequestMapping("/dossierDevice")
public class DossierDeviceController extends BaseController {

    @Autowired
    private IDossierDeviceService dossierDeviceService;

    @Autowired
    private IFavoritesService favoritesService;

    @Autowired
    private ICameraService cameraService;

    /**
     * 查询档案馆设备列表
     */
    @ApiOperation("查询设备列表")
    @GetMapping("/list")
    public TableDataInfo list(DossierDeviceVO dossierDevice) {
        startPage();
        List<DossierDeviceVO> list = dossierDeviceService.selectDossierDeviceList(dossierDevice);
        return getDataTable(list);
    }

    /**
     * 查询档案馆设备列表
     */
    @ApiOperation("选择设备列表")
    @GetMapping("cameraList")
    public AjaxResult getByGroupIdTree() {
        return AjaxResult.success(favoritesService.selectCameraListByFavoriteId(4621l));
    }

    @ApiOperation("通过Ip查询门禁Code")
    @GetMapping("guardStreamUrl")
    public Camera guardList(String ip) {
        return cameraService.getCameraByIp(ip);
    }

    @ApiOperation("摄像头cameraCode 获取设备流rtmp/flv/hls/spt")
    @GetMapping("/streamUrl")
    public AjaxResult queryStreamUrlByCameraCode(String cameraCode) {
        Object obj = cameraService.streamUrl(cameraCode);
        if (null == obj) {
            return AjaxResult.error("获取实时流异常");
        } else {
            return AjaxResult.success(obj);
        }
    }

    /**
     * 新增档案馆设备
     */
//    @ApiOperation("批量新增设备")
//    @PostMapping("batchAdd")
//    public AjaxResult batchAdd(@RequestBody DossierDeviceBatchAddDTO dto) {
//        return toAjax(dossierDeviceService.batchInsertDossierDevice(dto));
//    }

    /**
     * 新增档案馆设备
     */
    @ApiOperation("新增设备信息")
    @PostMapping
    public AjaxResult add(@RequestBody DossierDeviceDTO dossierDeviceAddDTO) {
        DossierDevice dossierDevice = new DossierDevice();
        BeanUtil.copyProperties(dossierDeviceAddDTO, dossierDevice);
        return AjaxResult.success(dossierDeviceService.insertDossierDevice(dossierDevice));
    }

    /**
     * 修改档案馆设备
     */
    @ApiOperation("修改设备信息")
    @PutMapping
    public AjaxResult edit(@RequestBody DossierDeviceDTO dossierDeviceUpdateDTO) {
        DossierDevice dossierDevice = new DossierDevice();
        BeanUtil.copyProperties(dossierDeviceUpdateDTO, dossierDevice);
        return AjaxResult.success(dossierDeviceService.updateDossierDevice(dossierDevice));
    }

    /**
     * 删除档案馆设备
     */
    @ApiOperation("删除设备")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(dossierDeviceService.deleteDossierDeviceByIds(ids));
    }

    /**
     * 控制设备开关
     */
    @ApiOperation("控制设备开关")
    @PostMapping("openOrClose")
    public AjaxResult openOrClose(@RequestBody DossierDeviceOpenOrCloseDTO dto) {
        boolean open = dossierDeviceService.openOrClose(dto);
        return AjaxResult.success(open ? "开启成功" : "关闭成功");
    }
}
